//@charset "utf-8";
// 表单页面交互
define(function(require, exports, module) {
	var CodeListModel = require('../models/codelist');
	var StockSearchModel = require('../models/stocksearch');
	var plateListModel = require('../models/platelist');
	var Util = external.CreateObject('Util');
	var BaseView = require('../views/base');
	var baseView = new BaseView();
	var FormView = BaseView.extend({
		nowPage: 1,
		dataCount: 0,
		el: 'body',
		keyCode: false,
		startIndex: undefined,
		dragTipFlag: 0,
		isMove: false, // 是否拖拽中
		events: {
			'mouseup .data-stock': 'clickRow',
			'mousedown .data-stock': 'initdrag',
			'mouseenter .data-stock': 'showDragTip',
			'mouseleave .data-stock': 'hideDragTip',
			'click .selectRows': 'checkboxSelect',
			'click #close': 'closeWebDlg',
			'click #title-explain': 'explain',
			'click #dynamic-title-explain': 'dynamicExplain',
			'click [view_click_stat]': 'statLog'
		},
		bind: function() {
			var self = this;

			// 全选/取消全选
			$('.btn-selectall').live('click', function() {
				if ($(this).is(":checked")) {
					self.selectAll();
				} else {
					self.unselectAll();
				}
				self.checkSelectStock();
			});

			$(document).mousemove(function(e) {
				self.drap(e);
			}).mouseup(function(e) {
				self.undrap(e);
			});

			// 键盘事件判断
			$(document).keydown(function(e) {
				self.keyCode = e.keyCode;
			}).keyup(function(e) {
				self.keyCode = false;
			});

			$(window).blur(function() {
				self.keyCode = false;
			});

		},
		emptyTable: function() {
			$('table').remove();
		},
		initdrag: function(e) {
			if(baseView.getQuery('plateid')){
				return;
			}
			if (this.keyCode) {
				return;
			}
			this.isMove = true;
			var codelist = this.getCodeList();
			var $dom = e.target;
			if (!$(e.target).hasClass('data-stock')) {
				$dom = $(e.target).parent('.data-stock');
			}
			if (codelist.length === 0) {
				var code = $dom.attr('data-stock');
				CodeListModel.setCodeList([code]);
			}
		},
		drap: function(e) {
			if (!this.isMove) {
				return;
			}
			var $drag = $('#drag-tip');

			// 实时拖拽
			$drag.css({
				top: e.clientY,
				left: e.clientX + 5
			});
			$drag.show();
		},
		undrap: function(e) {
			var self = this;
			if (!this.isMove) {
				return;
			}
			this.isMove = false;
			this.endDragPlate(e);

			var $dom = $(e.target);

			if (!$dom.hasClass('items')) {
				$dom = $dom.parents('.items');
			}


			if ($dom.length) {
				if ($dom.attr('data-type') === 'realtime') {
					self.showtip('无法添加到动态板块');
				} else if ($dom.attr('id') === 'new-plate') {
					$('#new-plate').click();
				} else {
					var plateId = $dom.attr('data-id');
					var codelist = self.getCodeList();
					if (codelist.length <= 0) {
						codelist = CodeListModel.getCodeListStr();
					}
					this.savePlateAdd(plateId, codelist, $dom);
					TA.log({
						id: "cop_581207d8_589"
					});
				}
			}
			$('#drag-tip').hide();
		},
		getCodeList: function() {
			var codelist = [];
			var $domsSelected = $('.data-stock-select');
			$.each($domsSelected, function(i, dom) {
				var code = $(dom).attr('data-stock');
				codelist.push(code);
			});
			return codelist;
		},
		clickRow: function(e) {
			var self = this;
			var $dom = $(e.target);
			var plateid = baseView.getQuery('plateid');
			if (!$dom.hasClass('data-stock')) {
				$dom = $dom.parents('.data-stock');
			}
			
			if ($dom.hasClass('data-stock-select')) {
				$dom.removeClass('data-stock-select');
				$dom.find('.selectRows').prop("checked", false);
				//COCO修改
				if (plateid) {
					var code = [$dom.attr('data-stock')];
					baseView.deletePlateStock(plateid, code);
					$('#tenstock').find('tr[data-stock='+$dom.attr('data-stock')+']').find('input').prop("checked", false);
				}

			} else {
				$dom.addClass('data-stock-select');
				$dom.find('.selectRows').prop("checked", true);

				if (plateid) {
					var code = [$dom.attr('data-stock')];
					self.addPlate(plateid, code);
					$('#tenstock').find('tr[data-stock='+$dom.attr('data-stock')+']').find('input').prop("checked", true);
				}
			}
			self.checkSelectStock();
		},
		addPlate: function(plateid, code) {

			CodeListModel.addCodeList(code);
			//$('#save_input').val($('html').html());
			var codelist = CodeListModel.getCodeListStr();

			baseView.savePlateAdd(plateid, codelist, $("#items-data-id-" + plateid));
		},
		selectShift: function(clickIndex) {
			var startIndex = this.startIndex;
			this.unselectAll(); // 重置了startindex
			this.startIndex = startIndex;
			startIndex = startIndex || $('.data-stock').eq(0).attr('data-index');

			var doms = '';
			if (startIndex > clickIndex) {
				var startIndexTemp = startIndex;
				startIndex = clickIndex;
				clickIndex = startIndexTemp;
				// startIndex ^= clickIndex;
				// clickIndex ^= startIndex;
				// startIndex ^= clickIndex;
			}
			for (var i = startIndex; i <= clickIndex; i++) {
				doms += '.data-stock[data-index=' + i + '],';
			}

			this.selectRows($(doms));

		},
		selectRows: function($dom) {

		},
		selectAll: function() {
			var self = this;
			var $allRom = $('.data-stock');
			$allRom.addClass('data-stock-select');
			var plateid = baseView.getQuery('plateid');
			var codes = [];
			$(".selectRows").each(function() {
				$(this).prop("checked", true);
				var stock = $(this).closest('tr').attr('data-stock');
				codes.push(stock);
			});

			if (plateid) {
				self.addPlate(plateid, codes);
			}
		},
		unselectAll: function() {
			var self = this;
			$('.data-stock-select').removeClass('data-stock-select');
			this.startIndex = undefined;
			var plateid = baseView.getQuery('plateid');
			var codes = [];
			$(".selectRows").each(function() {
				$(this).prop("checked", false);
				var stock = $(this).closest('tr').attr('data-stock');
				codes.push(stock);
			});
			if (plateid) {
				baseView.deletePlateStock(plateid, codes);
				CodeListModel.removeCodeList(codes);
				return;
			}
			CodeListModel.clearCodeList();

			CodeListModel.setCodeList([]);
		},
		showDragTip: function(e) {
			if(baseView.getQuery('plateid')){
				return;
			}
			var $tip = $('#tip');

			$tip.css({
				'top': e.clientY,
				'left': e.clientX
			});

			this.dragTipFlag = setTimeout(function() {
				$tip.show();
			}, 3000);
		},
		hideDragTip: function() {
			var $tip = $('#tip');

			clearTimeout(this.dragTipFlag);
			$tip.hide();
		},
		checkboxSelect: function(e) {
			var self = this;
			e = e || window.event;
			var $dom = $(e.currentTarget);
			if ($dom.is(":checked")) {
				$dom.prop("checked", false);
			} else {
				$dom.prop("checked", true);
			}
			self.checkSelectStock();
		},
		//关闭窗口
		closeWebDlg: function() {
			external.closeWebDlg();
			plateListModel.unregisterCustomBlockUpdatePush();
		},
		//验证是否有选中股票
		checkSelectStock: function() {
			var self = this;
			var codelist = self.getCodeList();
			if (baseView.getQuery('plateid')) {
				return;
			}
			if (codelist.length > 0) {
				$("#view_select_choose").show();
				$("#view_select_choose_name").removeClass("add-plate-lock");
			} else {
				$("#view_select_choose").hide();
				$("#view_select_choose_name").addClass("add-plate-lock");
			}
		},
		explain: function() {
			TA.log({
				id: 'cop_5811fb94_443'
			});
			Util.openURL('http://sp.10jqka.com.cn/liuyan/index/index/projectid/****/question/普通板块如何使用/', 9);
		},
		dynamicExplain: function () {
			TA.log({
				id: 'cop_5811fb94_443'
			});
			Util.openURL('http://sp.10jqka.com.cn/liuyan/index/index/projectid/****/question/动态板块如何使用/', 9);
		},
		statLog: function(e) {
			e = e || window.event;
			var id = $(e.currentTarget).attr('view_click_stat');
			TA.log({
				id: id
			});
		}
	});

	module.exports = FormView;
});
